package kr.hkit.androidcomplete02.ch12;

import java.util.ArrayList;

import kr.hkit.androidcomplete02.R;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class ListOfViews extends Activity {
	ArrayList<ListItem> arItem;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch12_listviewarraylist);

		arItem = new ArrayList<>();
		arItem.add(new ListItem(0, "제목", "입력내용", "", 0));
		arItem.add(new ListItem(1, "", "", "Push", R.drawable.ic_launcher));
		arItem.add(new ListItem(0, "이름을 입력하시오", "니 이름", "", 0));
		arItem.add(new ListItem(0, "나이도 입력하시오", "몇살이니", "", 0));
		arItem.add(new ListItem(1, "", "", "누르시오", R.drawable.ic_launcher));
		arItem.add(new ListItem(1, "", "", "업로드", R.drawable.ic_launcher));
		arItem.add(new ListItem(0, "주소", "어디 사니?", "", 0));
		
		MultiAdapter adapter = new MultiAdapter(this, arItem);
		
		ListView listView = (ListView) findViewById(R.id.ch12_listView);
		listView.setAdapter(adapter);

	}

	class ListItem {
		int type;
		String text;
		String editText;
		String btnText;
		int iconRes;

		public ListItem(int type, String text, String editText, String btnText,
				int iconRes) {
			super();
			this.type = type;
			this.text = text;
			this.editText = editText;
			this.btnText = btnText;
			this.iconRes = iconRes;
		}
	}

	class MultiAdapter extends BaseAdapter {
		LayoutInflater inflater;
		ArrayList<ListItem> arSrc;

		public MultiAdapter(Context context, ArrayList<ListItem> arItem) {
			this.inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			this.arSrc = arItem;
		}

		public int getCount() {
			return arSrc.size();
		}

		public Object getItem(int position) {
			return arSrc.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public int getItemViewType(int position) {
			return arSrc.get(position).type;
		}

		public int getViewTypeCount() {
			return 2;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				int res = 0;
				switch (arSrc.get(position).type) {
				case 0:
					res = R.layout.ch12_textedit;
					break;
				case 1:
					res = R.layout.ch12_btnicon;
					break;
				}
				convertView = inflater.inflate(res, parent, false);
			}

			switch (arSrc.get(position).type) {
			case 0:
				TextView tv = (TextView) convertView
						.findViewById(R.id.ch12_textedit_tv);
				tv.setText(arSrc.get(position).text);
				EditText edit = (EditText) convertView
						.findViewById(R.id.ch12_textedit_edit);
				edit.setText(arSrc.get(position).editText);
				break;
			case 1:
				Button btn = (Button) convertView
						.findViewById(R.id.ch12_btnicon_btn);
				btn.setText(arSrc.get(position).btnText);
				ImageView img = (ImageView) convertView
						.findViewById(R.id.ch12_btnicon_img);
				img.setImageResource(arSrc.get(position).iconRes);
				break;
			}

			return convertView;
		}

	}
}
